/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enteties.person.address;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import util.person.address.AddressDetails;

/**
 *
 * @author Gebruiker
 */
@Stateless
public class AddressFacade extends AbstractFacade<Address> implements AddressFacadeLocal {
    @EJB
    private CountryFacadeLocal countryFacade;
    @EJB
    private AddresstypeFacadeLocal addresstypeFacade;
    @PersistenceContext(unitName = "Liebaut-ejbPU")
    private EntityManager em;
    

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public AddressFacade() {
        super(Address.class);
    }
    
    @Override
    public Long createAddress(String street, String number, String postalCode, String bus, String location, boolean defaultAddress, Integer countryid, Long typeid){
        Country country = countryFacade.find(countryid);
        Addresstype type = addresstypeFacade.find(typeid);
        Address address = new Address(street, number, postalCode, bus, location, defaultAddress, country, type);
        create(address);
        return address.getId();
    }
    
    @Override
    public void editAddress(Long id, String street, String number, String postalCode, String bus, String location, boolean defaultAddress, Integer countryid, Long typeid){
        Address address = find(id);
        Country country = countryFacade.find(countryid);
        Addresstype type = addresstypeFacade.find(typeid);
        address.setStreet(street);
        address.setNumber(number);
        address.setPostalCode(postalCode);
        address.setBus(bus);
        address.setLocation(location);
        address.setDefaultAddress(defaultAddress);
        address.setCountryId(country);
        address.setAddressTypeid(type);
        edit(address);
    }
    
    @Override
    public void removeAddress(Long id){
        Address address = find(id);
        remove(address);
    }
    
    @Override
    public AddressDetails getAddressDetail(Long id){
        Address address = find(id);
        return convert(address);
        
    }
    
    public AddressDetails convert(Address address){
        return new AddressDetails(address.getId(), address.getStreet(),address.getNumber(), address.getPostalCode(), address.getBus(), address.getLocation(), address.getDefaultAddress(),address.getCountryId().getId(), address.getAddressTypeid().getId());
    }
    
}
